// ===========================================
// Simple effect to texture the given object
// ===========================================

float4x4 gWVP;
texture gTex;
float3 position = {300,384,-1500};
float SunSize = 1500;

sampler TexS = sampler_state {
	Texture = <gTex>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU  = WRAP;
    AddressV  = WRAP;
};

struct OutputVS {
    float4 posH   : POSITION0;
	float2 tex0   : TEXCOORD0;
};


OutputVS LensFlareVS(float4 posL : POSITION0, float2 tex0: TEXCOORD0) {
    // Zero out our output.
	OutputVS outVS = (OutputVS)0;	
	// we are already in screen space
	outVS.posH = posL;
	outVS.tex0 = tex0; 
    return outVS;
}

float4 LensFlarePS(float2 texCoord : TEXCOORD0) : COLOR {    
	float4 col = 0;
	float4 ScreenPosition = float4(position,1);//mul(position,gWVP);
    float scale = ScreenPosition.z;
    ScreenPosition.xyz /= ScreenPosition.w;
    ScreenPosition.x = ScreenPosition.x/2.0f+0.5f;
    ScreenPosition.y = (-ScreenPosition.y/2.0f+0.5f);
	float2 coord;
        
    float size = SunSize / scale;
                    
    float2 center = ScreenPosition.xy;

    coord = .5 - (texCoord - center) / size * .5;
    col += (pow(tex2D(TexS,coord),2) * 1) * 2;   
	col.a = 0.2;
	return col;
    //return tex2D(TexS, texCoord);
}

technique LensFlareTech {

    pass P0 {
        vertexShader = compile vs_2_0 LensFlareVS();
        pixelShader  = compile ps_2_0 LensFlarePS();
    }
}
